//
//  LinkedList.cpp
//  forJob
//
//  Created by 杜锋 on 15/4/22.
//  Copyright (c) 2015年 bupt. All rights reserved.
//

#include "LinkedList.h"
TreePtr convert_tree_to_linkedlist(TreePtr root)
{
    TreePtr lastNode = NULL;
    convert_helper(root,lastNode);
    while(lastNode->left)lastNode = lastNode->left;
    return lastNode;
}
//二叉搜索树转双向链表
void convert_helper(TreePtr t,TreePtr lastNode)
{
    if(t== NULL) return;
    //左子树
    TreePtr currentNode = t;
    if(currentNode->left)
        convert_helper(currentNode->left,lastNode);
    currentNode->left = lastNode;
    if(lastNode)
        lastNode->right = currentNode;
    lastNode = currentNode;
    //右子树
    if(currentNode->right)
        convert_helper(currentNode->right,lastNode);
}
